package BIO.udp;
/*
 * @Author  Wrial
 * @Date Created in 14:41 2020/4/9
 * @Description
 */

import java.io.IOException;
import java.net.*;

public class UDPServer {
    public static void main(String[] args) throws IOException {
        UDPServer udpServer = new UDPServer(8090);
        udpServer.service();
    }

    private DatagramSocket datagramSocket;

    public UDPServer(int port) throws SocketException {
        init(port);
    }

    private void init(int port) throws SocketException {
        datagramSocket = new DatagramSocket(port);
        System.out.println("server init ");
    }

    private void service() throws IOException {

        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        while (true) {
            datagramSocket.receive(packet);
            System.out.println("receive from " + packet.getAddress());
            String msg = new String(packet.getData());
            if (msg == "") break;
            System.out.println("msg == " + msg);
        }


    }
}
